package org.electric.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.codehaus.jackson.map.ObjectMapper;

import org.electric.bean.productBean;
import org.electric.service.ProductService;
import org.electric.util.Page;
import org.electric.util.WriterUtil;


/**
 * Servlet implementation class ProductServlet
 */
//@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 ProductService productser=new ProductService();  
	 	 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("_method");
		
		if("all".equals(method)){
                  SeAllProduct(request, response);
		}
		if("add".equals(method)){
           AddProduct(request, response);
	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	
public String imp(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException, ServletException{
    	//存储路径
    	String savePath = request.getServletContext().getRealPath("/WEB-INF/static/image");
    	
    	//获取上传的文件集合
    	Collection<Part> parts = request.getParts();
    	String fileName = null;
    	String tip="没有数据";
    	
    	//上传单个文件
    	if (parts.size()==1) {
    		//通过表单file控件(<input type="file" name="file">)的名字直接获取Part对象		
        	Part part=request.getPart("file");
        	String header = part.getHeader("content-disposition");
        	
        	String filelongName =header.split(";")[header.split(";").length-1];
        	//获取文件名.xx
    		 fileName = filelongName.
    				split("\\\\")[filelongName.split("\\\\").length-1].
    				substring(0, filelongName.split("\\\\")
    						[filelongName.split("\\\\").length-1].length()-1);//;	
            //把文件写到指定路径
    		
    		
    		
	    part.write(savePath+File.separator+fileName);//File.separator(//)
        	tip="上传成功";		
    }
    	String path=fileName;
    	PrintWriter out = response.getWriter();
    	out.println(tip);
    	out.flush();
    	out.close();
		return path;
  	 
    }

public void SeAllProduct(HttpServletRequest request, HttpServletResponse response) throws IOException{
	Page pa=new Page();
	productBean p=new productBean();
//	System.out.println(productser.AllCount(p.getLicense_id()));
	pa.setTotalNum(productser.AllCount(p.getLicense_id()));

	int nowcount = Integer.parseInt(request.getParameter("now"));
	System.out.println("当前页"+nowcount);
	pa.setPageNo(nowcount);
	List<productBean> list = productser.smpAll(p,pa);
	System.out.println("这是一个集合1"+list);
	Map<String ,Object> map=new HashMap<String ,Object>();
	map.put("all",pa.getTotalPage());
	map.put("now",pa.getPageNo());
    map.put("list", list);	

    ObjectMapper om=WriterUtil.writeTopage();
		 
	om.writeValue(response.getOutputStream(),map);   		
      
}
public void AddProduct(HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalStateException, ServletException{
	 productBean p=new productBean();
	 int addpcount=productser.addProduct(p);
	 String path=imp(request, response);
	  p.setImage(path);
	
	Map<String ,Object> map=new HashMap<String ,Object>();
	if(addpcount!=0){
		map.put("flag",1);
	}else{
		map.put("flag",0);
	}
	
	 ObjectMapper om=WriterUtil.writeTopage();
	om.writeValue(response.getOutputStream(),map);  
}


}
